package com.personal.gateway.web;

import com.alibaba.fastjson.JSONObject;
import com.personal.gateway.constant.LoginUserInfoConstant;
import com.personal.gateway.dao.SavePointDao;
import com.personal.gateway.service.JobService;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/job")
public class JobController {

    @Resource
    private JobService jobService;

    @Resource
    private LoginUserInfoConstant loginUserInfoConstant;

    @GetMapping("/startNoSavePoint")
    public String startNoSavePoint(ServerHttpRequest request, String jobName) {
        return jobService.startNoSavePoint(jobName);
    }

    @GetMapping("/startWithSavePoint")
    public String startWithSavePoint(ServerHttpRequest request, String jobName, String filePath) {
        return jobService.startWithSavePoint(jobName, filePath);
    }

    @GetMapping("/stopWithSavePoint")
    public String stopWithSavePoint(ServerHttpRequest request, String jobName) {
        return jobService.stopWithSavePoint(jobName);
    }

    @GetMapping("/getSavePoint")
    public List<SavePointDao> getSavePoint(ServerHttpRequest request, String jobName) {
        return jobService.getSavePoint(jobName);
    }

    /**
     * 执行命令
     */
    @PostMapping("/execute")
    public String execute(ServerHttpRequest request, @CookieValue("token") String token, @RequestBody JSONObject obj) {
        String user = loginUserInfoConstant.getUserByToken(token);
        return jobService.execute(obj, user);
    }

}
